(function($){
	var regexp = /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/;   
	$.fn.validation = function(opts){
		var id = $(this).attr("id");
		var settings = $.extend({
			url: '',//上传的地址
			defaultMsg:'',
			errorMsg : '',
			id:'',
			submitBtns : []
		}, opts || {});
		var outThat = $(this);
		var input = "<input type='hidden' id='isSubmit' name='isSubmit'/>";
		$('body').append(input);
		for(var i = 0 ; i < settings.submitBtns.length ; i ++) {
			$("#" + settings.submitBtns[i]).click(function(event){
				var website = outThat.val();
				if(website == '') {
					$("#" + id + "_tip").html("<span style='color:red'>"+settings.defaultMsg+"</span>");
					$("#isSubmit").val(1);
					return false;
				} else {
					if(!regexp.test(website)) {
						outThat.focus();
						$("#" + id + "_tip").html("<span style='color:red'>"+settings.errorMsg+"</span>");
						return false;
					} else {
						$.getJSON(settings.url + "?website="+website + "&id=" + settings.id,function(data){
							if(data.resposecode == '200'){
								$("#" + id + "_tip").html("<span style='color:green'>"+data.message+"</span>");
								$("#isSubmit").val(2);
								return true;
							} else {
								outThat.focus();
								$("#isSubmit").val(1);
								$("#" + id + "_tip").html("<span style='color:red'>"+data.message+"</span>");
								return false;
							}
						});
					}
				}
			});
		}
		$(this).on('keyup',function() {
			var website = $(this).val();
			if(website == '') {
				$("#isSubmit").val(1);
				$("#" + id + "_tip").html("<span style='color:red'>"+settings.defaultMsg+"</span>");
				return false;
			}
		});
		$(this).on('blur',function() {
			var website = $(this).val();
			var that = $(this);
			if(website != '' && website != null && website != 'undefined' ){ 
				if(!regexp.test(website)) {
					that.focus();
					$("#isSubmit").val(1);
					$("#" + id + "_tip").html("<span style='color:red'>"+settings.errorMsg+"</span>");
					return false;
				} else {
					$.getJSON(settings.url + "?website="+website,function(data){
						if(data.resposecode == '200'){
							$("#isSubmit").val(2);
							$("#" + id + "_tip").html("<span style='color:green'>"+data.message+"</span>");
							return true;
						} else {
							that.focus();
							$("#isSubmit").val(1);
							$("#" + id + "_tip").html("<span style='color:red'>"+data.message+"</span>");
						}
					});
				}
			} else {
				$("#isSubmit").val(1);
				$("#" + id + "_tip").html("<span style='color:red'>"+settings.defaultMsg+"</span>");
				return false;
			}
		});
	}
})(jQuery);